<template>
  <div class="login_container">
    <div class="login-box">
      <div class="avatar_box">
        <img src="../assets/1.webp" alt="" />
      </div>

      <!-- 表单区 -->

      <el-form
        ref="loginFormRef"
        label-width="0px"
        class="login_form"
        :rules="loginFormRules"
        :model="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            @keypress.enter.native="login"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary"  @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 这是登录表单的数据对象

      loginForm: {
        username: '',
        password: '',
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名！', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码！', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '长度在 3 到 16 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //点击重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    //登录
    login() {
      this.$refs.loginFormRef.validate(async (vaild) => {
        // 失败 返回fasle
        if (!vaild) {
          return
        }
        // const result= await this.$http.post('login',this.loginForm)
        // console.log(result); //返回的是promise对象 要使用async 和await 将数据弄成普通对象，可以针对数据了
        const { data: res } = await this.$http.post('login', this.loginForm)
        //  console.log(res.meta.status);
        // if(res.meta.status!==200){
        //   return console.log('失败');

        //   }
        // console.log('成功');

    
        if (res.meta.status !== 200) return this.$message.error('失败！')

        this.$message.success('成功！')

        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
  },
}
    

</script>

<style>
.login_container {
  height: 100%;
  background-color: #e2e2d5;
}
.login-box {
  border-radius: 20px;
  width: 400px;
  height: 315px;
  position: absolute;
  left: 50%;
  top: 50%;
  background-color: white;
  transform: translate(-50%, -50%);
}
.avatar_box {
  width: 130px;
  height: 130px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -65%);
  box-shadow: 0 0 10px rgb(60, 60, 60);

  border-radius: 50%;
  background-color: white;
}
.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>